
package cn.manyufun.myf.widget.actionsheet.other_comment;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.manyufun.myf.R;
import cn.manyufun.myf.widget.actionsheet.MaskView;


public class ActionSheetComment extends RelativeLayout {
    protected final static long durationMillis = 200;
    protected WindowManager windowManager;
    protected GestureDetector gestureDetector;
    protected MaskView maskView;
    protected LinearLayout actionSheetView;

    public ActionSheetComment(Context context) {
        super(context);
        initialize();
    }

    public ActionSheetComment(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public ActionSheetComment(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    protected void initialize() {
        maskView = new MaskView(getContext(), this);
        maskView.setCanCancel(true);
        maskView.setDurationMillis(durationMillis);
        maskView.setOnMaskListener(new MaskView.MaskListener() {
            @Override
            public void onShow() {
            }

            @Override
            public void onHide() {
                hide();
            }
        });

//        actionSheetView = new LinearLayout(getContext());
//        actionSheetView.setOrientation(LinearLayout.VERTICAL);
//        int pad = (int) UnitConverter.applyDimension(getContext(), TypedValue.COMPLEX_UNIT_DIP, 10);
//        actionSheetView.setPadding(pad, 0, pad, pad);
////        actionSheetView.setBackgroundResource(R.drawable.actionsheet_background);
//        //这个layout盖在mask上面，所以想要没有背景就设置背景全透明
//        actionSheetView.setBackgroundColor(0xffffffff);
//        actionSheetView.setVisibility(View.GONE);
//        LayoutParams rlp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
//        addView(actionSheetView, rlp);


        final LayoutInflater inflater = LayoutInflater.from(getContext());
        actionSheetView = (LinearLayout) inflater.inflate(R.layout.comment_layout, null);
        LayoutParams rlp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        addView(actionSheetView, rlp);

        windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        gestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                hide();
                return super.onSingleTapUp(e);
            }
        });
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (KeyEvent.KEYCODE_BACK == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
                    hide();
                    return true;
                }
                return false;
            }
        });
        setFocusable(true);
        setFocusableInTouchMode(true);
    }


    public void show(String title, final CommentCallBack callBack) {
        if (getParent() == null) {
            WindowManager.LayoutParams wlp = new WindowManager.LayoutParams();
            wlp.type = WindowManager.LayoutParams.TYPE_APPLICATION;
            wlp.format = PixelFormat.TRANSPARENT;
            wlp.gravity = Gravity.LEFT | Gravity.TOP;
            wlp.width = LayoutParams.MATCH_PARENT;
            wlp.height = LayoutParams.MATCH_PARENT;
            windowManager.addView(this, wlp);
        }

        maskView.show();

        ImageView close = (ImageView) actionSheetView.findViewById(R.id.comment_btn_cancel);
        ImageView submit = (ImageView) actionSheetView.findViewById(R.id.comment_btn_ok);
        final EditText editText = (EditText) actionSheetView.findViewById(R.id.comment_edittext);
        TextView titleview = (TextView) actionSheetView.findViewById(R.id.title);
        titleview.setText(title);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.close();
            }
        });
        submit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString();
                callBack.submit(content);
            }
        });

        actionSheetView.setVisibility(VISIBLE);
        editText.requestFocus();
//        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        inputManager.showSoftInput(editText, 0);
//        actionSheetView.clearAnimation();
//        TranslateAnimation an = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
//        an.setDuration(durationMillis);
//        actionSheetView.startAnimation(an);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
//                               editText.requestFocus();
                               InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(editText, 0);
                           }
                       },
                100);

//        editText.setFocusable(true);

//        InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        inputManager.showSoftInput(editText, 0);
    }

    public void hide() {
        maskView.hide();
        actionSheetView.clearAnimation();
        TranslateAnimation an = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
        an.setDuration(durationMillis);
        an.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                actionSheetView.setVisibility(View.GONE);
                if (getParent() != null)
                    windowManager.removeView(ActionSheetComment.this);
            }
        });
        actionSheetView.startAnimation(an);
    }
}
